package com.example.module_motorcircle.post.repository

import com.example.lib_net.api.Api
import com.example.lib_net.constant.API
import com.example.lib_net.model.api.BaseModel
import com.example.lib_net.model.api.ReportTypeModel
import com.example.module_motorcircle.post.model.CollectRequestModel
import com.example.module_motorcircle.post.model.ShieldSaveRequestModel
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.flow.flowOn
import kotlinx.coroutines.flow.map
import okhttp3.RequestBody
import retrofit2.http.Body
import retrofit2.http.GET
import retrofit2.http.POST
import retrofit2.http.Query
import javax.inject.Inject

/**
 * @author yaotianxue
 * @description
 * @date 2025/2/13
 */
class PostRepository @Inject constructor() {
    @Inject
    lateinit var api: Api
    /**
     * 获得帖子数据
     */
    fun getPostPage(category:Int?,page:Int,pageSize:Int) = flow {
        val baseModel = api.getPostPage(category,page,pageSize)
        emit(baseModel)
    }.flowOn(Dispatchers.IO)

    /**
     * 点赞和取消帖子
     */
    fun likeOrCancel(postId:String) = flow {
        emit(api.validatePost(postId)) //校验帖子
    }.map {
        if(it.data){
            api.likeOrCancel(postId)
        }else{
            it
        }
    }.flowOn(Dispatchers.IO)

    /**
     * 收藏和取消帖子
     */
    fun collectOrCancel(ifCollect:Int,postId:String) = flow {
        emit(api.validatePost(postId)) //校验帖子
    }.map {
        if(it.data){
            val requestModel = CollectRequestModel(ifCollect = ifCollect,thirdId = postId,type = 1)
            api.collectOrCancel(requestModel.toRequestBody())
        }else{
            it
        }
    }.flowOn(Dispatchers.IO)

    /**
     * 关注
     */
    fun followMember(memberId:String) = flow {
        emit(api.followMember(memberId))
    }.flowOn(Dispatchers.IO)

    /**
     * 取消关注
     */
    fun followCancelMember(memberId:String) = flow {
        emit(api.followCancelMember(memberId))
    }.flowOn(Dispatchers.IO)


    /**
     * 屏蔽帖子
     */
    fun shieldSave(id:String,type:Int) = flow {
        val model = ShieldSaveRequestModel(id,type)
        emit(api.shieldSave(model.toRequestBody()))
    }.flowOn(Dispatchers.IO)

    /**
     * 删除帖子
     */
    fun deletePost(postsId:String) = flow{
        emit(api.deletePost(postsId))
    }.flowOn(Dispatchers.IO)


}